<template>
  <div></div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import { useUserStore } from '@/store/modules/user'
import { useRoute, useRouter } from 'vue-router'
import { ROUTER_NAME } from '@/constant'
import { localSave } from "@/utils/common";
export default defineComponent({
  name: 'AuthorizeLogin',
  setup() {
    const route = useRoute()
    const router = useRouter()
    const userStore = useUserStore()
    // 登录
    function login() {
      const fullPath = route.fullPath
      const applicationId = fullPath.match(/app_(\d+)/)?.[1]
      const { uid: uuid, token, tenantId, productId, redirectUrl } = route.query
      if (!token) {
        return
      }
      const appUserStore: any = { 'app-user-store': {}, 'al__USER_INFO': {} };
      if (tenantId && productId) {
        userStore.tenantId = tenantId
        userStore.productId = productId
        appUserStore['app-user-store']['tenantId'] = tenantId;
        appUserStore['app-user-store']['productId'] = productId;
      }
      if (token && uuid) {
        userStore.appid = 'product-container'
        userStore.token = token
        userStore.uuid = uuid
        appUserStore['app-user-store']['appid'] = 'product-container'
        appUserStore['app-user-store']['token'] = token
        appUserStore['app-user-store']['uuid'] = uuid
      }
      localSave(`app_${applicationId}`, appUserStore)
      if (redirectUrl) {
        window.location.href = `/${decodeURIComponent(redirectUrl)}`
        return
      }
      router.push({ name: ROUTER_NAME.PAGE_ROOT })
    }

    login()
    return {
      login
    }
  }
})
</script>

<style lang="less" scoped>
.alert-tip {
  width: 70%;
  margin: 50px auto;
}

.login-button {
  padding-left: 0;
  padding-right: 0;
}
</style>
